package testes.vSys.jUnitTestes;

import java.util.Collection;
import java.util.Iterator;

import junit.framework.TestCase;
import testes.vSys.bean.Brand;
import testes.vSys.bean.CarType;
import testes.vSys.bean.Color;
import testes.vSys.bean.Fuel;
import testes.vSys.bean.Location;
import testes.vSys.bean.Model;
import testes.vSys.bean.Vehicle;
import testes.vSys.dao.DAOFactory;
import testes.vSys.dao.IVehicleDao;
import testes.vSys.database.DatabaseController;


public class _hsql_VehicleDaoTest extends TestCase {

	private IVehicleDao vehicleDAO;
	
	protected void setUp() throws Exception {
		super.setUp();

		if (DatabaseController.initiate("/home/cc2004/ra032923/home-ext/Projeto/vehiclesys-mc686/VSysProject/VSys/WEB-INF/databaseConnection.properties"))
			System.out.println("[VSys] Base de da/os inicializada com sucesso.");
		else
			System.out.println("[VSys] Problemas na inicializacao da base de dados.");
		
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testCreateVehicle() {
		
		vehicleDAO = DAOFactory.getVehicleDao();
		
		Vehicle vehicle = new Vehicle();
	
		vehicle.setPlate("CAI0003");
		vehicle.setYear(2002);
		vehicle.setValue(15000);
		vehicle.setLocation(new Location("SP", "Campinas"));
		vehicle.setModel( new Model(1, new Brand(1, "Chevrolet"), new CarType(1, "Utilitario"), "Montana" ));
		vehicle.setFuel(new Fuel(1, "Gasolina"));
		vehicle.setColor( new Color(1, "Vermelha"));
		
		Vehicle vehicleTest = this.vehicleDAO.createVehicle(vehicle);
		assertNull("Insercao de veiculo repitido", vehicleTest);	
		
		vehicle.setPlate("yyyyyy9");
		vehicle.setYear(1999);
		vehicle.setValue(10000);
		vehicle.setLocation(new Location("SP","Tatui"));
		vehicle.setFuel(new Fuel(1, "Gasolina"));
		vehicle.setColor(new Color(1,"Verde"));
		vehicle.setModel(new Model(1,new Brand(1,"Ford"),new CarType(1,"qq"),"Focus"));
		
		vehicleTest = this.vehicleDAO.createVehicle(vehicle);
		assertEquals("Comparação entre os veiculos", vehicleTest.getPlate(), vehicle.getPlate());	
	
		assertTrue("Removendo um veiculo existente", this.vehicleDAO.removeVehicle(vehicleTest.getId()));
	}

	public void testGetAvaiableColors() {
	
	vehicleDAO = DAOFactory.getVehicleDao();
	
	Collection<Color> colorTest = (Collection<Color>) this.vehicleDAO.getAvaiableColors();
	
	boolean var = false;
	boolean marrom = true;
	
	Iterator iMov = colorTest.iterator();
	
	while(iMov.hasNext()){
		Color f = (Color) iMov.next();
		if(f.getColor().compareTo("Vermelha") == 0)
			var = true;
		if(f.getColor().compareTo("Agua") == 0)
			marrom = false;
	}
		
	assertTrue("Verifica Cor", var);
	assertTrue("Verifica Cor- Marrom", marrom);

 	}

	public void testGetAvaiableFuels() {
		vehicleDAO = DAOFactory.getVehicleDao();
		
		Collection<Fuel> fuelTest = (Collection<Fuel>) this.vehicleDAO.getAvaiableFuels();
			boolean var = false;
			boolean agua = true;
			
			Iterator iMov = fuelTest.iterator();
			
			while(iMov.hasNext()){
				Fuel f = (Fuel) iMov.next();
				if(f.getFuel().compareTo("Alcool") == 0)
					var = true;
				if(f.getFuel().compareTo("Agua") == 0)
					agua = false;
			}
				
			assertTrue("Verifica combustivel", var);
			assertTrue("Verifica combustivel- Agua", agua);
 	}

	public void testGetVehicleByPlate() {
		Vehicle vehicle = new Vehicle();
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicle.setPlate("CAI0003");
		vehicle.setYear(2002);
		vehicle.setValue(15000);
		vehicle.setLocation(new Location("SP", "Campinas"));
		vehicle.setModel( new Model(2, new Brand(1, "Chevrolet"), new CarType(1, "Utilitario"), "Montana" ));
		vehicle.setFuel(new Fuel(2, "Gasolina"));
		vehicle.setColor( new Color(5, "Vermelha"));
		vehicle.setYear(2002);
		vehicle.setValue(15000);
		vehicle.setLocation(new Location("SP", "Campinas"));
		vehicle.setModel( new Model(2, new Brand(1, "Chevrolet"), new CarType(1, "Utilitario"), "Montana" ));
		vehicle.setFuel(new Fuel(2, "Gasolina"));
		vehicle.setColor( new Color(5, "Vermelha"));
				
		Vehicle vehicleTest = this.vehicleDAO.getVehicleByPlate(vehicle.getPlate());
		
		assertEquals("Comparação entre os veiculos",vehicle.getPlate(), vehicleTest.getPlate());
				
		vehicleTest = this.vehicleDAO.getVehicleByPlate("NNNl6666");
		
		assertNull("Veiculo nao existente (Retorno Nulo)",vehicleTest);
	
	}

	public void testGetVehicleById(){
		
		Vehicle vehicle = new Vehicle();
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicle.setPlate("CAI0003");
				
		Vehicle vehicleTest = this.vehicleDAO.getVehicleByPlate(vehicle.getPlate());
		
		Vehicle vehicleId = this.vehicleDAO.getVehicleById(vehicleTest.getId());
		
		assertEquals("Comparação entre carros",vehicle.getPlate(), vehicleId.getPlate());
		
		vehicleId = this.vehicleDAO.getVehicleById(8777777);
		assertNull("Id não existe", vehicleId);
		
	}

	public void testGetVehicles() {
		vehicleDAO = DAOFactory.getVehicleDao();
		
		Collection<Vehicle> vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicles();
		
		assertNotNull(vehicleTest);
		
		vehicleTest = (Collection<Vehicle>) this.vehicleDAO.getVehicles("model");
		
		assertNotNull(vehicleTest);
		
	}

	public void testRemoveVehicle() {
		
		Vehicle vehicle = new Vehicle();
		
		vehicleDAO = DAOFactory.getVehicleDao();
		
		vehicle.setPlate("41144411");
		vehicle.setYear(1999);
		vehicle.setValue(10000);
		vehicle.setLocation(new Location("SP","Tatui"));
		vehicle.setFuel(new Fuel(1, "Gasolina"));
		vehicle.setColor(new Color(1,"Verde"));
		vehicle.setModel(new Model(1,new Brand(1,"Ford"),new CarType(1,"qq"),"Focus"));
				
		Vehicle vehicleTest = this.vehicleDAO.createVehicle(vehicle);
		
		assertTrue("Removendo um veiculo existente", this.vehicleDAO.removeVehicle(vehicleTest.getId()));
		
		vehicle.setPlate("CAI0003");
		
		vehicleTest = this.vehicleDAO.getVehicleByPlate(vehicle.getPlate());
		
		assertFalse("Removendo veiculo inexistente", this.vehicleDAO.removeVehicle(vehicleTest.getId()));
		
	}

	public void testUpdateVehicle() {
	
		Vehicle vehicle = new Vehicle();
		
		vehicleDAO = DAOFactory.getVehicleDao();
		
		assertFalse("Editando um veiculo nulo", this.vehicleDAO.updateVehicle(vehicle));
		
		vehicle.setPlate("CAI0008");
		vehicle.setYear(2002);
		vehicle.setValue(15000);
		vehicle.setLocation(new Location("SP", "Campinas"));
		vehicle.setModel( new Model(2, new Brand(1, "Chevrolet"), new CarType(1, "Utilitario"), "Montana" ));
		vehicle.setFuel(new Fuel(2, "Gasolina"));
		vehicle.setColor( new Color(5, "Vermelha"));
						
		assertFalse("Editando um veiculo inexistente", this.vehicleDAO.updateVehicle(vehicle));
		
		Vehicle vehicleTest = this.vehicleDAO.getVehicleByPlate("CAI0003");
		
		vehicleTest.setValue(25000);
		
		assertFalse("Editando um veiculo existente", this.vehicleDAO.updateVehicle(vehicleTest));
		
					
	}

	public void testGetColorById() {
	
	vehicleDAO = DAOFactory.getVehicleDao();
	
	assertEquals("Comparando Cores", "Preto", this.vehicleDAO.getColorById(0).getColor());
	
	assertNull("Cor inexistente", this.vehicleDAO.getColorById(15));
	}

	public void testGetFuelById() {
		vehicleDAO = DAOFactory.getVehicleDao();
		
		assertEquals("Comparando Combustivel", "Alcool", this.vehicleDAO.getFuelById(0).getFuel());
		
		assertNull("Combustivel inexistente", this.vehicleDAO.getFuelById(15));
	}

	public void testGetVehicle(){
	
		vehicleDAO = DAOFactory.getVehicleDao();
		Collection<Vehicle> vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "CAI0003","","","","", "","","","","","");
		assertNotNull(vehicleTest);
		
		
		Vehicle vehicle = new Vehicle();
		vehicle.setPlate("CAI0003");
				
		Vehicle vehicleTemp = this.vehicleDAO.getVehicleByPlate(vehicle.getPlate());
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle(Integer.toString(vehicleTemp.getId()), "","","","","", "","","","","","");
		assertNotNull(vehicleTest);
	
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","2002","","","", "","","","","","");
		assertNotNull(vehicleTest);
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","","2006","","", "","","","","","");
		assertNotNull(vehicleTest);
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","","","15000","", "","","","","","");
		assertNotNull(vehicleTest);
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","","","","500000", "","","","","","");
		assertNotNull(vehicleTest);
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","","","","", "Vermelha","","","","","");
		assertNotNull(vehicleTest);
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","","","","", "","Montana","","","","");
		assertNotNull(vehicleTest);
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","","","","", "","","Chevrolet","","","");
		assertNotNull(vehicleTest);
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","","","","", "","","","Gasolina","","");
		assertNotNull(vehicleTest);
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","","","","", "","","","","Campinas","");
		assertNotNull(vehicleTest);
		
		vehicleDAO = DAOFactory.getVehicleDao();
		vehicleTest = (Collection<Vehicle>)this.vehicleDAO.getVehicle("", "","","","","", "","","","","","SP");
		assertNotNull(vehicleTest);
		
	}
}
